import json
from utils.lg import logger


class ConfigParser:
    """配置文件解析器"""
    def __init__(self, file_path=""):
        self.configs = {}
        self.configs_file_path = ""
        if file_path:
            self.read(file_path)

    def read(self, file_path):
        logger.info(f"从配置文件中获取配置信息: {file_path}")
        with open(file_path, 'r', encoding='utf-8') as f:
            self.configs = json.load(f)
        self.configs_file_path = file_path

    def write(self, **kwargs):
        """
        user_name: str
        user_psw: str
        is_maxsize: bool
        is_headless: bool
        loading_delay: float
        answering_delay: float
        finished_delay: float
        log_level: str "DEBUG"/"INFO"
        log_rotation: int
        """
        for key, value in kwargs.items():
            self.configs[key] = value
        with open(self.configs_file_path, 'w', encoding='utf-8') as f:
            json.dump(self.configs, f)


if __name__ == '__main__':
    parser = ConfigParser()
    parser.read(r"D:\Codes\python\lry-assistant\configs.json")
    parser.write(answering_delay=1.5)
